﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MuseLibrary.EncodingHelpers
{
    public class BitReader
    {
        public BitReader()
        {
            ResetState();
        }

        public void ResetState()
        {
            _byteIndex = 0;
            _bitIndex = 0;
        }

        private int _byteIndex;
        private int _bitIndex;

        public bool ReadBit(List<int> data, out bool value)
        {
            if (_bitIndex == 8)
            {
                _bitIndex = 0;
                _byteIndex++;
            }

            var b = data[_byteIndex];
            var bitShift = (1 << (7 - _bitIndex++));
            value = (b & bitShift) == bitShift;
            return ByteIndex == data.Count - 1 && BitIndex == 8;
        }

        public int ByteIndex
        {
            get { return _byteIndex; }
        }

        public int BitIndex
        {
            get { return _bitIndex; }
        }
    }
}
